package com.xjtu.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;


/**
 * 初始化通道配置
 */
public class WSServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        //获取管道
        ChannelPipeline pipeline = channel.pipeline();
        //将请求和应答消息编码/解码为http协议消息
        pipeline.addLast(new HttpServerCodec())

                .addLast(new ChunkedWriteHandler())

                //对httpMessage聚合成request或response
                .addLast(new HttpObjectAggregator(1024*64))

                //一分钟心跳支持
                .addLast(new IdleStateHandler(40,45,50))

                //自定义的空闲状态检测的handler
                .addLast(new HeartBeatHandler())

                //监听的websocket请求地址
                .addLast(new WebSocketServerProtocolHandler("/chat"))

                //自定义聊天处理器
                .addLast(new ChatHandler());
    }
}
